package com.doctor.windflower_doctor.adapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.actionBeen.UserControlBeen;
import com.doctor.windflower_doctor.activity.TenantService;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.http.parser.impl.JsonHelper;
import com.doctor.windflower_doctor.myuser.tools.HttpclientHelper;
import com.doctor.windflower_doctor.util.Constent;

public class UserControlAdapter extends BaseAdapter implements Constent {
	private List<UserControlBeen> list;
	private Context context;
	private boolean isShow = false;
	private Dialog dialog;
	private ImageView rightImageView;
	private SharedPreferencesDB db;

	public boolean isShow() {
		return isShow;
	}

	public void setShow(boolean isShow) {
		this.isShow = isShow;
		notifyDataSetChanged();
	}

	public UserControlAdapter(Context context, ImageView rightImageView) {
		this.context = context;
		this.list = new ArrayList<UserControlBeen>();
		this.rightImageView = rightImageView;
		dialog = new Dialog(context);
		dialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		db = SharedPreferencesDB.getInstance(context);
	}

	public void listRemove(int position) {
		list.remove(position);
		notifyDataSetChanged();
	}

	public void listAddAll(List<UserControlBeen> list) {
		this.list.clear();
		this.list.addAll(list);
		
	}

	public void listAdd(UserControlBeen userControlBeen) {
		list.add(userControlBeen);
		notifyDataSetChanged();
	}

	public void listReplace(String groupName, int position) {
		UserControlBeen userControlBeen = list.get(position);
		userControlBeen.setGroupName(groupName);
		list.set(position, userControlBeen);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.usercontrol_view, null);
			viewHolder.groupName = (TextView) convertView
					.findViewById(R.id.groupname_textView);
			viewHolder.number = (TextView) convertView
					.findViewById(R.id.number_textView);
			viewHolder.reName_Button = (ImageView) convertView
					.findViewById(R.id.rename_button);
			viewHolder.remove_Button = (ImageView) convertView
					.findViewById(R.id.remove_button);
			viewHolder.arrows_Group = (ImageView) convertView
					.findViewById(R.id.arrows_group);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if (isShow) {
			convertView.setClickable(false);
		} else {
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(context, TenantService.class);
					Bundle mBundle = new Bundle();
					mBundle.putSerializable("arr", (Serializable) list);
					mBundle.putString("id", list.get(position).getId());
					mBundle.putString("name", list.get(position).getGroupName());
					intent.putExtras(mBundle);
					context.startActivity(intent);
				}
			});

		}
		if (position != 0) {
			if (isShow) {

				viewHolder.reName_Button
						.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								View view = LayoutInflater.from(context)
										.inflate(R.layout.replace_dialogue,
												null);
								Button replace_sure_button = (Button) view
										.findViewById(R.id.replace_sure_button);
								Button replace_cancel_button = (Button) view
										.findViewById(R.id.replace_cancel_button);
								final EditText replace_editText = (EditText) view
										.findViewById(R.id.replace_editText);
								replace_sure_button
										.setOnClickListener(new OnClickListener() {

											@Override
											public void onClick(View v) {
												// TODO
												// Auto-generated
												// method stub
												if (replace_editText.getText()
														.toString().trim()
														.equals("")) {
													Toast.makeText(context,
															"请填写重命名",
															Toast.LENGTH_SHORT)
															.show();
												} else {
													new User_Control_Asynk_Replace(
															replace_editText
																	.getText()
																	.toString()
																	.trim(),
															position, list.get(
																	position)
																	.getId()).execute(URL
															+ EDIT_URL
															+ db.getDocotorId());
												}

											}
										});
								replace_cancel_button
										.setOnClickListener(new OnClickListener() {

											@Override
											public void onClick(View v) {
												// TODO
												// Auto-generated
												// method stub
												dialog.dismiss();
											}
										});
								dialog.setContentView(view);
								dialog.show();
							}

						});
				viewHolder.remove_Button.setVisibility(View.VISIBLE);

				viewHolder.remove_Button
						.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								View view = LayoutInflater.from(context)
										.inflate(R.layout.delete_ok, null);
								TextView groupName = (TextView) view
										.findViewById(R.id.groupname_textView);
								groupName.setText(list.get(position)
										.getGroupName() + " " + "?");
								Button make_sure_button = (Button) view
										.findViewById(R.id.make_sure_button);
								Button cancel_button = (Button) view
										.findViewById(R.id.cancel_button);
								make_sure_button
										.setOnClickListener(new OnClickListener() {

											@Override
											public void onClick(View v) {
												// TODO
												// Auto-generated
												// method stub
												new User_Control_Asynk_Delete(
														list.get(position)
																.getId(),
														position).execute(URL
														+ DELETE_URL
														+ db.getDocotorId());
												dialog.dismiss();
											}
										});
								cancel_button
										.setOnClickListener(new OnClickListener() {

											@Override
											public void onClick(View v) {
												// TODO
												// Auto-generated
												// method stub

												dialog.cancel();
											}
										});
								dialog.setContentView(view);
								dialog.show();

							}
						});
				viewHolder.number.setVisibility(View.INVISIBLE);
				viewHolder.arrows_Group.setVisibility(View.INVISIBLE);
				viewHolder.reName_Button.setVisibility(View.VISIBLE);
				viewHolder.remove_Button.setVisibility(View.VISIBLE);
			} else {
				viewHolder.reName_Button.setVisibility(View.INVISIBLE);
				viewHolder.remove_Button.setVisibility(View.INVISIBLE);
				viewHolder.number.setVisibility(View.VISIBLE);
				viewHolder.arrows_Group.setVisibility(View.VISIBLE);
			}
		} else {
			viewHolder.reName_Button.setVisibility(View.INVISIBLE);
			viewHolder.remove_Button.setVisibility(View.INVISIBLE);
			viewHolder.number.setVisibility(View.VISIBLE);
			viewHolder.arrows_Group.setVisibility(View.VISIBLE);
		}
		viewHolder.groupName.setText(list.get(position).getGroupName());
		viewHolder.number.setText(list.get(position).getNumberOfUser());
		return convertView;
	}

	class ViewHolder {
		private TextView groupName;
		private TextView number;
		private ImageView reName_Button;
		private ImageView remove_Button;
		private ImageView arrows_Group;
	}

	class User_Control_Asynk_Replace extends AsyncTask<String, Void, String> {
		private String groupName;
		private int position;
		private String id;

		private User_Control_Asynk_Replace(String groupName, int position,
				String id) {
			this.position = position;
			this.groupName = groupName;
			this.id = id;
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			return HttpclientHelper.postReplaceGroupName(groupName, id,
					params[0]);
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result != null) {
				if (JsonHelper.userControl_Delete_Json(result).equals("true")) {
					SystemApplication.getInstance().isTypeChange = true;
					listReplace(groupName, position);
					setShow(false);
					rightImageView.setImageResource(R.drawable.edit_button);
				} else {
					Toast.makeText(context, "分组名称不能重复命名", Toast.LENGTH_SHORT)
							.show();
				}
			} else {
				Toast.makeText(context, "请连接网络...", Toast.LENGTH_SHORT).show();
			}

			dialog.cancel();
		}

	}

	class User_Control_Asynk_Delete extends AsyncTask<String, Void, String> {
		private String id;
		private int position;

		public User_Control_Asynk_Delete(String id, int position) {
			this.id = id;
			this.position = position;
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			return HttpclientHelper.postRemoveGroupName(id, params[0]);
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result != null) {
				if (JsonHelper.userControl_Delete_Json(result).equals("true")) {
					SystemApplication.getInstance().isTypeChange = true;
					listRemove(position);
					setShow(false);
					rightImageView.setImageResource(R.drawable.edit_button);

				} else {
					Toast.makeText(context, "服务器错误", Toast.LENGTH_SHORT).show();
				}
			} else {
				Toast.makeText(context, "请连接网络...", Toast.LENGTH_SHORT).show();
			}

		}

	}
}
